import modelExtend from 'dva-model-extend'
import { pageModel } from '@/common/model/BaseModel'
import {
  listUser,
  lockUser,
  // getUser,
  checkUnique,
  //
  getUserList,
  saveUser,
  getUserById,
  delUser,
  getUsersByOrgId,
} from '../pages/Sys/account/services/AccountService'
import { listDep } from '../pages/Sys/department/services/department'
import { list } from '../pages/Sys/role/services/RoleService'

export default modelExtend(pageModel, {
  namespace: 'account',
  state: {
    currentUser: {},
    depData: [],
    roleData: [],
    currentItem: {},
    modalType: '',
    selectedRowKeys: [],
    formValues: {},
    //
    usersByOrgId: [],
  },
  effects: {
    // 查询当前用户
    // *fetchCurrent({ payload }, { call, put }) {
    //   const response = yield call(getUser, payload)
    //   yield put({
    //     type: 'saveCurrentUser',
    //     payload: response.data,
    //   })
    // },
    // 校验编码唯一性
    *checkUnique({ payload }, { call }) {
      return yield call(checkUnique, payload)
    },
    // 右侧按条件查询
    *fetchUser({ payload }, { call, put }) {
      const response = yield call(listUser, payload)
      yield put({
        type: 'updateState',
        payload: {
          list: response.data.records,
          pagination: {
            showSizeChanger: false,
            showQuickJumper: true,
            showTotal: total => `共 ${total} 条`,
            total: response.data.total,
            current: response.data.current,
          },
        },
      })
    },
    // 编辑按钮
    *edit({ payload }, { call, put }) {
      const response = yield call(getUserById, payload)
      if (response && response.success) {
        yield put({
          type: 'updateState',
          payload: {
            modalType: 'edit',
            currentItem: response.data,
          },
        })
      }
    },
    // 保存提交
    *save({ payload }, { call, put }) {
      const response = yield call(saveUser, payload)
      if (response && response.success) {
        yield put({ type: 'fetch' })
        yield put({
          type: 'updateState',
          payload: {
            modalType: '',
            currentItem: {},
            list: response.data.data,
            pagination: {
              showSizeChanger: false,
              showQuickJumper: true,
              showTotal: total => `共 ${total} 条`,
              total: response.data.resourceSize,
              current: response.data.currentPage,
              pageSize: response.data.pageSize,
            },
          },
        })
      }
    },
    // 查询
    *fetch({ payload }, { call, put }) {
      // 查询数据
      const userData = yield call(getUserList, payload)
      // const treeData = yield call(listOrgByAttr, { status: '0001' })
      yield put({
        type: 'updateState',
        payload: {
          list: userData.data.data,
          pagination: {
            showSizeChanger: false,
            showQuickJumper: true,
            showTotal: total => `共 ${total} 条`,
            total: userData.data.resourceSize,
            current: userData.data.currentPage,
            pageSize: userData.data.pageSize,
          },
          // orgData: treeData.data,
        },
      })
    },

    // 部门数据
    *fetchDepData(_, { call, put }) {
      const depData = yield call(listDep)
      yield put({
        type: 'updateState',
        payload: {
          depData: depData.data,
        },
      })
    },

    /**
     * 角色列表
     * @param {*} _
     * @param {*} param1
     */
    *fetchRoleData(_, { call, put }) {
      const roleData = yield call(list, {
        size: 100,
        page: 1,
      })
      yield put({
        type: 'updateState',
        payload: {
          roleData: roleData.data.records,
        },
      })
    },

    // 切换锁定状态
    *lockSwitch({ payload }, { call, put }) {
      const response = yield call(lockUser, payload)
      if (response && response.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: response.data.records,
          },
        })
      }
    },
    // 删除
    *remove({ payload }, { call, put }) {
      const response = yield call(delUser, payload)
      if (response && response.success) {
        yield put({
          type: 'updateState',
          payload: {
            list: response.data.data,
            pagination: {
              showSizeChanger: false,
              showQuickJumper: true,
              showTotal: total => `共 ${total} 条`,
              total: response.data.resourceSize,
              current: response.data.currentPage,
              pageSize: response.data.pageSize,
            },
            // selectedRowKeys: [],
          },
        })
      }
    },

    *fetchUsersByOrgId({ payload, callback }, { put, call }) {
      const res = yield call(getUsersByOrgId, payload)
      if (res && res.success) {
        yield put({
          type: 'updateState',
          payload: {
            usersByOrgId: res.data,
          },
        })

        if (callback && typeof callback === 'function') {
          callback()
        }
      }
    },
  },
})
